package cn.echoedu.control;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.echoedu.command.base.FailureParam;
import cn.echoedu.command.base.SuccessParam;
import cn.echoedu.constants.Role;
import cn.echoedu.entity.Student;
import cn.echoedu.entity.Teacher;
import cn.echoedu.entity.User;
import cn.echoedu.entity.receivecommand.LoginReceive;
import cn.echoedu.util.L;

public class LoginControl extends BaseControl{
	private final String T = "LoginControl---------->";
	
	public void doLogin(HttpServletRequest request,
			HttpServletResponse response, LoginReceive loginreceive) throws SQLException {
		// TODO Auto-generated method stub
		final String M="doLogin***********>>";
		L.p(T+M);
        User user=getUser(request,response,loginreceive);
       if(isNull(user)){
			L.p(T+M+"user is null&&&&&&&&");
			sendFailure(request, response);
			return;
		}
		savedRequestMessage(request,loginreceive,user);
		validateRole(request,response,loginreceive,user);
     
	}
	private void validateRole(HttpServletRequest request,
			HttpServletResponse response, LoginReceive loginreceive,User user) throws SQLException{

		if(user.getRole()==Role.student){
			validateStudent(request,response,loginreceive);
		}else if(user.getRole()==Role.teacher)
		{
			validateTeacher(request,response,loginreceive);
		}
	}
	private void validateStudent(HttpServletRequest request,
			HttpServletResponse response, LoginReceive loginreceive) throws SQLException{
		String token = loginreceive.getMIME()+loginreceive.getApiKey()
				+loginreceive.getUser_number();
		Student student=getStudentModel().queryByUserNumberAndPassword(
				loginreceive.getUser_number()+"",loginreceive.getPassword());
		if(isNull(student)){
			sendFailure(request,response);
		}else{
			sendSuccess(request, response,student,token);
		}
	}
	private void validateTeacher(HttpServletRequest request,
			HttpServletResponse response, LoginReceive loginreceive) throws SQLException{
		String token = loginreceive.getMIME()+loginreceive.getApiKey()
				+loginreceive.getUser_number();
		Teacher teacher=getTeacherModel().queryByUserNumberAndPassword(
				loginreceive.getUser_number()+"",loginreceive.getPassword());
		if(isNull(teacher)){
			sendFailure(request, response);
		}else{
			sendSuccess(request,response,teacher,token);
		}
	}
	private User getUser(HttpServletRequest request,
			HttpServletResponse response, LoginReceive loginreceive){
		L.p(T+"getUser");
		User user=null;
		try {
			user = getUserModel().queryAllByNumber(loginreceive.getUser_number());
			if(isNull(user)){
				L.p(T+"getUser------user is null&&&&&&");
				 return null;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			L.p(T+"user is null SQLException");
			e.printStackTrace();
		}
//		 L.p(T+"---------final----------\nuserid:"+user.getUserid()+"\n"+user.getUsernumber()
//	        		+"\n"+user.getRole()+"\n-----------------");
//			
		return user;
	}
	private boolean isNull(Object user){
		return user==null?true:false;
	}
	private void savedRequestMessage(HttpServletRequest request,
			LoginReceive loginreceive,User user){
		getRequestMessageControl().savedRequestMessage(request,loginreceive,user);
	}
	private void sendFailure(HttpServletRequest request,
			HttpServletResponse response){
		FailureParam failureparam = new FailureParam();
		failureparam.setRequest(request);
		failureparam.setResponse(response);
		getResponseService().doLoginFailure(failureparam);
	}
	private void sendSuccess(HttpServletRequest request,
			HttpServletResponse response,Object param,String token){
		SuccessParam successparam = new SuccessParam();
		successparam.setRequest(request);
		successparam.setResponse(response);
		successparam.setParam(param);
		successparam.setToken(token);
		getResponseService().doLogin(successparam);
	}
	
}
